package com.cat.simulation;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-unguarded-cells-in-the-grid/?envType=problem-list-v2&envId=simulation
 * @create 2025/8/29 11:21
 * @since JDK17
 */

public class Solution05 {
    public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
        char[][] arr = new char[m][n];
        int ans = m * n;
        int[][] dire = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (int[] guard : guards) {
            arr[guard[0]][guard[1]] = 'g';
        }
        for (int[] wall : walls) {
            arr[wall[0]][wall[1]] = 'w';
        }
        ans -= guards.length + walls.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr[i][j] != 'g') {
                    continue;
                }
                for (int k = 0; k < 4; k++) {
                    for (int x = i, y = j; x >= 0 && x < n && y >= 0 && y < m; ) {
                        if (arr[x][y] != 0) {
                            break;
                        }
                        arr[x][y] = 1;
                        ans--;
                        x += dire[k][0];
                        y += dire[k][1];
                    }
                }
            }
        }
        return ans;
    }
}
